package com.draven.mall.controller;

import com.draven.mall.common.ApiResponse;
import com.draven.mall.entity.DispatchConfig;
import com.draven.mall.service.DispatchConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * 配送调度配置控制器
 */
@RestController
@RequestMapping("/api/admin/dispatch")
public class DispatchConfigController {

    private static final Logger log = LoggerFactory.getLogger(DispatchConfigController.class);

    @Autowired
    private DispatchConfigService dispatchConfigService;

    /**
     * 获取所有配送调度配置
     */
    @GetMapping("/configs")
    public ApiResponse getAllConfigs() {
        try {
            List<DispatchConfig> configs = dispatchConfigService.getAllConfigs();
            return ApiResponse.success(configs);
        } catch (Exception e) {
            log.error("获取调度配置列表异常", e);
            return ApiResponse.error("获取调度配置失败: " + e.getMessage());
        }
    }

    /**
     * 获取当前激活的调度配置
     */
    @GetMapping("/active-config")
    public ApiResponse getActiveConfig() {
        try {
            DispatchConfig config = dispatchConfigService.getActiveConfig();
            if (config == null) {
                return ApiResponse.error("当前没有激活的调度配置");
            }
            return ApiResponse.success(config);
        } catch (Exception e) {
            log.error("获取激活的调度配置异常", e);
            return ApiResponse.error("获取当前配置失败: " + e.getMessage());
        }
    }

    /**
     * 激活指定调度配置
     */
    @PostMapping("/activate")
    public ApiResponse activateConfig(@RequestParam Long configId) {
        try {
            boolean success = dispatchConfigService.activateConfig(configId);
            if (success) {
                return ApiResponse.success("配置激活成功");
            } else {
                return ApiResponse.error("配置激活失败");
            }
        } catch (Exception e) {
            log.error("激活调度配置异常", e);
            return ApiResponse.error("激活配置失败: " + e.getMessage());
        }
    }

    /**
     * 添加调度配置
     */
    @PostMapping("/add")
    public ApiResponse addConfig(@RequestBody DispatchConfig config) {
        try {
            // 设置创建和更新时间
            Date now = new Date();
            config.setCreatedTime(now);
            config.setUpdatedTime(now);
            
            boolean success = dispatchConfigService.addConfig(config);
            if (success) {
                return ApiResponse.success("配置添加成功");
            } else {
                return ApiResponse.error("配置添加失败");
            }
        } catch (Exception e) {
            log.error("添加调度配置异常", e);
            return ApiResponse.error("添加配置失败: " + e.getMessage());
        }
    }

    /**
     * 更新调度配置
     */
    @PostMapping("/update")
    public ApiResponse updateConfig(@RequestBody DispatchConfig config) {
        try {
            // 设置更新时间
            config.setUpdatedTime(new Date());
            
            boolean success = dispatchConfigService.updateConfig(config);
            if (success) {
                return ApiResponse.success("配置更新成功");
            } else {
                return ApiResponse.error("配置更新失败");
            }
        } catch (Exception e) {
            log.error("更新调度配置异常", e);
            return ApiResponse.error("更新配置失败: " + e.getMessage());
        }
    }

    /**
     * 删除调度配置
     */
    @PostMapping("/delete")
    public ApiResponse deleteConfig(@RequestParam Long configId) {
        try {
            boolean success = dispatchConfigService.deleteConfig(configId);
            if (success) {
                return ApiResponse.success("配置删除成功");
            } else {
                return ApiResponse.error("不能删除激活状态的配置");
            }
        } catch (Exception e) {
            log.error("删除调度配置异常", e);
            return ApiResponse.error("删除配置失败: " + e.getMessage());
        }
    }
} 